package projectmanagement.managementtool.UserServices;

import projectmanagement.managementtool.IdGenerator;
import projectmanagement.models.Project;
import projectmanagement.models.Task;
import projectmanagement.models.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/*
 *
 * The UserServiceImpl Class provides methods to interact with User model
 *
 * @author Sergey Parakhin (sergey.parahin@gmail.com)
 *
 */
public class UserServiceImpl implements UserService {
    private Map<Integer, User> users = new HashMap<Integer, User>();

    public User createUser(String name, int age, String position, String technologies) {
        User user = new User(IdGenerator.generate(User.class),
                name, age, position, technologies);
        users.put(user.getId(), user);
        return user;
    }

    public void assignTask(int userId, Task task) {
        task.setUser(users.get(userId));
    }

    public void assignProject(int userId, Project project) {
        project.addUser(users.get(userId));
    }

    public User getUserById(int id) {
        return users.get(id);
    }

    public Collection<User> getUsersList() {
        return users.values();
    }

    public User getUserByName(String name) {
        for (User user : users.values())
            if (name.equals(user.getName()))
                return user;
        return null;
    }
}